package com.rock.sys.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.eova.common.base.BaseController;
import com.eova.model.User;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;

import cn.hutool.core.util.StrUtil;

public class RockWebPageController extends BaseController {

	/**
	 * 页面请求
	 */
	public void index() {

		this.setAttr("errMsg", "");
		String pageCode = getPara("code");
		
		//未指定页面编码
		if(StrUtil.isBlank(pageCode)) {
			this.setAttr("errMsg", "页面加载失败：未传递页面编码");
			render("/ui_rock/main.html");
			return;
		}

//		User user = this.getUser();// 租户ID
//		String tenantId = Db.queryStr("select tenant_id from t_user where id=?", user.getStr("id"));
		
		Record page = new Record();
		List<Record> mainList = new ArrayList<Record>();
		//请求主页，找到目前正在启用的主页code
		if("main".equals(pageCode)) {
			mainList = Db.find("select * from t_s_webpage where type=1 and is_use=1");
		} else {
			mainList = Db.find("select * from t_s_webpage where code=? and is_use=1",pageCode);
		}

		if(mainList.size() == 0) {
			this.setAttr("errMsg", "页面加载失败：未找到页面配置或页面未启用");
			render("/ui_rock/main.html");
			return;
		}
		page = mainList.get(0);
		
		List<Record> pageCompRcs = Db.find("select * from t_s_webpage_comp where page_id=? and is_show=1 order by sort_no",page.getStr("id"));

		List<HashMap<String, Object>> pageComps = new ArrayList<HashMap<String, Object>>();
		for (Record comp : pageCompRcs) {
			pageComps.add(buildCompData(comp));
		}

		this.setAttr("pageCode", pageCode);
		this.setAttr("pageComps", pageComps);
		
		render("/ui_rock/main.html");
	}
	

	/**
	 * 组装组件数据
	 * 
	 * @param c
	 * @param pageComp
	 * @return
	 */
	public static HashMap<String, Object> buildCompData(Record comp) {

		HashMap<String, Object> dataMap = new HashMap<String, Object>();

		dataMap.put("code", comp.getStr("code"));
		dataMap.put("type", comp.getStr("type"));

		//默认将comp_params的值返回给前端
		dataMap.put("data", JSONObject.parseObject(comp.getStr("comp_params")));
		
		// 卡片
		if (1 == comp.getInt("type")) {
			List<HashMap<String, String>> map = getCardData(comp);
			dataMap.put("data", map);
		}
		return dataMap;
	}

	/**
	 * 组装卡片数据
	 * @param comp
	 * @return
	 */
	private static List<HashMap<String, String>> getCardData(Record comp) {
		
		List<HashMap<String, String>> mapList = new ArrayList<HashMap<String, String>>();
		
		JSONObject compParams = JSONObject.parseObject(comp.getStr("comp_params"));
		JSONArray cardArray = (JSONArray)compParams.get("cardList");
		List<HashMap> cardList = JSONObject.parseArray(cardArray.toJSONString(), HashMap.class);
		for(Map card : cardList) {
			String sql = (String) card.get("sql");
			if(!StrUtil.isBlank(sql)) {
				//动态查询sql结果
				String value = Db.queryStr(sql);
				card.put("dvalue", value);
			}
			mapList.add((HashMap<String, String>) card);
		}
		return mapList;
	}

}
